<?php
include('header.php');
	
	// Find all authors
	$cursor = q('author')->find();
	
	// If there has been submitted a new post in the addnewpost form:
	if(isset($_POST['addnewpost'])) {
		$array = array();
		$array['post_title'] = addslashes($_POST['post_title']);
		$array['post'] = addslashes($_POST['post_content']);
		$array['tags'] = explode(", ", $_POST['post_tags']);
		
		// We want to insert a reference to the author in the post document.
		// MongoDBRef::create creates a new database reference
		// the getName(void) function returns the name of the collection
		$array['author'] = MongoDBRef::create(q('author')->getName(), new MongoId($_POST['authors']));
		
		// We could also use the MongoDB standard date/time notation: new MongoDate() which will return yyyy-mm-dd tt:mm:ss
		$array['date'] = time();
		
		$add_post_start = timeit();		
		q('post')->insert($array);
		$add_post_end = timeit();
		$add_post_time = number_format($add_post_end - $add_post_start, 15);			
		$postid = $array['_id'];
		
		redirect_to('posts.php?postid='.$postid.'&status=success&msg=newpost&querytime='.$add_post_time.'&query=added_post');
	}

?>


<div id="container">
	<div id="main">
		<div id="respond">
			<h3>Add new post</h3>
			<div class="formcontainer">	
				<form id="addNewPost" name="addnewpost" action="add.php" method="post" enctype="multipart/form-data">
					<div class="commsec">
						
						<div id="form-section-email" class="form-section">
							<div class="form-label"><label for="post_title">Post title</label> <span class="required">*</span></div>
							<div class="form-input"><input id="post_title" name="post_title" type="text" value="" size="30" maxlength="50" tabindex="4" /></div>
						</div>
						<div id="form-section-author" class="form-section">
							<div class="form-label"><label for="author">Author</label> <span class="required">*</span></div>
							<div class="form-input">
								<select name="authors">
									<?php
									while($authors = $cursor->getNext()) {
										echo '<option value='.$authors['_id'].'>'.$authors['name'].'</option>';
									} ?>
								</select>
							</div>
						</div>
						
						<div id="form-section-email" class="form-section">
							<div class="form-label"><label for="post_title">Post tags (Seperate by comma)</label> <span class="required">*</span></div>
							<div class="form-input"><input id="post_title" name="post_tags" type="text" value="" size="30" maxlength="50" tabindex="4" /></div>
						</div>
						
					</div>

					<div id="form-section-comment" class="form-section">
						<div class="form-label"><label for="comment">Post content</label></div>
						<div class="form-textarea"><textarea id="comment" name="post_content" cols="45" rows="8" tabindex="6"></textarea></div>
					</div>
					
					<div class="form-submit"><input id="submit" name="addnewpost" type="image" src="images/submit2.png" value="Add new post" tabindex="7" /></div>
				</form>
			</div>
		</div>
		<div class="cleardiv"></div>
	</div>
	<div class="sidebar">
		<div id="search_main" class="block">
			<form method="post" name="search" action="posts.php" enctype="multipart/form-data">
				<div>
					<input type="text" class="field" name="search" id="search" value="Search post content ..." onfocus="if (this.value == 'Search post content ...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search post content ...';}"/>
					<input type="image" src="images/sbtn.png" class="submit" name="submit"/>
				</div>
			</form>
		</div>
		
		<div class="widget">
			<div class="widgettitle">
				Navigation
			</div>
			<div class="widgetconten">
				<p><a href="posts.php">View all posts</a></p>
			</div>
		</div>		
		
		<div class="widget">
			<div class="widgettitle">
				Admin section
			</div>
			<div class="widgetconten">
				<p><a href="add.php">Add new post</a></p>
				<p><a href="tests.php">Run Tests</a></p>
			</div>
		</div>

		<div class="widget">
			<div class="widgettitle">
				MongoDB Query Information
			</div>
			<div class="widgetconten">
				<p>Information</p>
				<p>Query time</p>
			</div>
		</div>

	</div>
</div>

<?php
include('footer.php');
?>